package com.qiusuo0226.self.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiusuo0226.common.constant.NumConstant;
import com.qiusuo0226.common.util.AlgorithmTool;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.mysql.property.MybatisPlusProperty;
import com.qiusuo0226.self.mapper.BillSourceMapper;
import com.qiusuo0226.self.service.BillSourceService;
import com.qiusuo0226.self.pojo.entity.BillSourceDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 账单资金源表 逻辑实现层
 * @author qiusuo
 * @date 2023/8/5 23:54
 */
@Service
public class BillSourceServiceImpl extends ServiceImpl<BillSourceMapper, BillSourceDO> implements BillSourceService {

    @Autowired
    private MybatisPlusProperty mybatisPlusProperty;

    @Override
    public JsonResultModel add(String creater, String sourceName, Double sourceAmount) {
        int effectRows = baseMapper.add(AlgorithmTool.snowflake(mybatisPlusProperty.getGlobalConfig().datacenterId, mybatisPlusProperty.getGlobalConfig().workerId),
                                        new Date(),
                                        creater,
                                        sourceName,
                                        sourceAmount);
        return effectRows > NumConstant.N0 ? JsonResultModel.success("新增成功") : JsonResultModel.failure("新增失败");
    }

    @Override
    public JsonResultModel edit(String creater, String sourceIid, String sourceName, Double sourceAmount) {
        int effectRows = baseMapper.edit(sourceIid, sourceName, sourceAmount);
        return effectRows > 0 ? JsonResultModel.success("编辑成功") : JsonResultModel.failure("编辑失败");
    }

    @Override
    public JsonResultModel remove(String sourceIids) {
        List<String> iids = Arrays.asList(sourceIids.split(","));
        return baseMapper.deleteBatchIds(iids) > 0 ? JsonResultModel.success("删除成功") : JsonResultModel.failure("删除失败");
    }

    @Override
    public JsonResultModel list(String creater) {
        QueryWrapper<BillSourceDO> wrapper = new QueryWrapper<>();
        wrapper.select("iid, source_name sourceName, source_amount sourceAmount");
        wrapper.eq("deleted", "0");
        wrapper.eq("creater", creater);
        List<Map<String, Object>> list = baseMapper.selectMaps(wrapper);
        return JsonResultModel.success("获得账单资金源列表成功", list);
    }


    @Override
    @Transactional()
    public boolean modifyAmount(String sourceIid, Double amount) {
        return false;
    }

}
